<?php

namespace App\Models;

use App\Util\Tool;
use Illuminate\Database\Eloquent\Model;
use App\Models\Course;
use Illuminate\Support\Facades\DB;
class CoursesSection extends Model
{
    protected $table = 'c_courses_section';


    public function Courses()
    {
        return $this->belongsTo('App\Models\Course','courses_id');
    }

    public function doAddCoursesSection($data)
    {
        try {
            DB::beginTransaction(); // 开始事务
//                更新
            if (isset($data['id']) && !empty($data['id'])) {
                $redata = $this->getCourseSectionInfo($data['id']);
                if(empty($redata)){
                    return '该章节不存在!';
                }
                $redata->courses_id = isset($data['courses_id']) ? $data['courses_id'] : $redata->courses_id;
                $redata->courses_video = isset($data['courses_video']) ? $data['courses_video'] : $redata->courses_video;
                $redata->courses_title = isset($data['courses_title']) ? $data['courses_title'] : $redata->courses_title;
                $redata->user_id = isset($data['user_id']) ? $data['user_id'] : $redata->user_id;
                $res = $redata->save();
            } else {
                //添加
                $this->courses_id = $data['courses_id'];
                $this->courses_video = $data['courses_video'];
                $this->courses_title = $data['courses_title'];
                $this->user_id = isset($data['user_id']) ? $data['user_id'] : 0;
                $res = $this->save();
                (new Course())->where([['id','=',$data['courses_id']]])->increment("nums");
            }
            if ($res) {
                DB::commit(); // 提交事务
                return ['section_id' => isset($data['id']) ? $data['id'] : $this->id];
            } else {
                return '添加失败';
                DB::rollback(); // 回滚事务
            }
        } catch (Exception $e) {
            DB::rollback(); // 回滚事务
        }
    }


    public function getCourseSectionInfo($course_id)
    {
        return $this->where("id", '=', $course_id)->first();
    }

    /****
     * @param $result
     * @param $course_id
     * @return array|string
     *
     * IOS批量添加章节
     */
    public function doAddCoursesSectionIos($result, $course_id)
    {
        $num = 0;
        $data = [];
        $section = json_decode($result['section'], true);
        foreach ($section as $k => $v) {
            $data[$k]['courses_id'] = $course_id;
            $data[$k]['courses_video'] = $v['courses_video'];
            $data[$k]['courses_title'] = $v['courses_title'];
            $data[$k]['user_id'] = $v['user_id'];
            $data[$k]['created_at'] = date("Y-m-d H:i:s", time());
            $data[$k]['updated_at'] = date("Y-m-d H:i:s", time());
            $num++;
        }
        $res = DB::table('c_courses_section')->insert($data);
        $inf = (new Course())->where([['id', '=', $course_id]])->first();
        if ($inf->free_num < $num) {   //判断是否收费
            $is_pay = 1;
        } else {
            $is_pay = 0;
        }
        $inf->nums = $num;
        $inf->lesson_isbuy = $is_pay;
        $inf->save();
//        (new Course())->where([['id', '=', $course_id]])->update(['nums' => $num]);
        if ($res) {
            return ['section_id' => $res];
        }
        return '添加失败';
    }


    /***
     * @param $result
     * @param $course_id
     * @return array|string
     *
     */
    public function doAddCoursesSectionAndroid($result, $course_id)
    {
        $section = $this->handelCombinationData($result);
        $num = 0;
        $data = [];
//            $section = json_decode($result['section'],true);
        foreach ($section as $k => $v) {
            $data[$k]['courses_id'] = $course_id;
            $data[$k]['courses_video'] = $v['courses_video'];
            $data[$k]['courses_title'] = $v['courses_title'];
            $data[$k]['user_id'] = $result['user_id'];
            $data[$k]['created_at'] = date("Y-m-d H:i:s", time());
            $data[$k]['updated_at'] = date("Y-m-d H:i:s", time());
            $num++;
        }
        $res = DB::table('c_courses_section')->insert($data);
        $inf = (new Course())->where([['id', '=', $course_id]])->first();
        if ($inf->free_num < $num) {   //判断是否收费
            $is_pay = 1;
        } else {
            $is_pay = 0;
        }
        $inf->nums = $num;
        $inf->lesson_isbuy = $is_pay;
        $inf->save();
//        (new Course())->where([['id', '=', $course_id]])->update(['nums' => $num]);
        if ($res) {
            return ['section_id' => $res];
        }
        return '添加失败';
    }

    /**
     * @param $param
     * @return array
     * 处理android传输数组数据
     */
    public function handelCombinationData($param)
    {
        $name = Tool::handelString($param['section_name']);
        $url = Tool::handelString($param['section_url']);
        $arrs = [];
        foreach ($name as $k => $v) {
            $arrs[$k]['courses_title'] = $v;
            foreach ($url as $kk => $vv) {
                if ($k == $kk) {
                    $arrs[$k]['courses_video'] = $vv;
                }
            }
        }
        return $arrs;
    }

}
